<template>
  <el-card header="服务组管理">
    <div class="app-container">
      <el-form :inline="true" :model="queryParam">
        <el-form-item label="服务名">
          <el-input
            v-model="queryParam.QueryKey"
            placeholder="服务名"
            style="width: 200px"
            @change="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="default"
            @click="reset()"
          >重置</el-button>
          <el-button
            type="success"
            @click="add()"
          >新增服务组</el-button>
        </el-form-item>
      </el-form>
    </div>
    <w-table
      :data="groups"
      :columns="columns"
      :is-paging="true"
      row-key="Id"
      :paging="paging"
      @load="loadTable"
    >
      <template slot="isEnable" slot-scope="e">
        <el-switch
          :value="e.row.IsEnable"
          inactive-text="停用"
          active-text="启用"
          @change="(val) => setStatus(e.row, val)"
        />
      </template>
      <template slot="action" slot-scope="e">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-view"
          title="编辑服务组"
          circle
          @click="edit(e.row)"
        />
        <el-button
          v-if="e.row.IsEnable == false && e.row.UseNum ==0"
          size="mini"
          icon="el-icon-delete"
          type="danger"
          title="删除服务组"
          circle
          @click="drop(e.row)"
        />
      </template>
    </w-table>
    <phyGroupEdit :id="id" :visible.sync="visible" @save="closeEdit" />
  </el-card>
</template>

<script>
import phyGroupEdit from './components/phyGroupEdit'
import * as phyGroupApi from '@/customForm/api/phyGroup'
export default {
  components: {
    phyGroupEdit
  },
  data() {
    return {
      id: null,
      visible: false,
      groups: [],
      columns: [
        {
          key: 'GroupName',
          title: '组名 ',
          align: 'left',
          minWidth: 200
        },
        {
          key: 'SysTypeVal',
          title: '系统类型',
          align: 'left',
          minWidth: 200
        },
        {
          key: 'Show',
          title: '备注',
          align: 'left',
          minWidth: 100
        },
        {
          key: 'UseNum',
          title: '使用数',
          align: 'right',
          minWidth: 120
        },
        {
          key: 'IsEnable',
          title: '表单状态',
          align: 'center',
          slotName: 'isEnable',
          minWidth: 140
        },
        {
          key: 'Action',
          title: '操作',
          align: 'left',
          width: '180px',
          slotName: 'action'
        }
      ],
      paging: {
        Size: 20,
        Index: 1,
        SortName: 'Id',
        IsDesc: true,
        Total: 0
      },
      queryParam: {
        QueryKey: null
      }
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  mounted() {
    this.reset()
  },
  methods: {
    search() {
      this.paging.Index = 1
      this.loadTable()
    },
    edit(item) {
      this.id = item.Id
      this.visible = true
    },
    add() {
      this.id = null
      this.visible = true
    },
    closeEdit(isRefresh) {
      if (isRefresh) {
        this.reset()
      }
    },
    async setStatus(row, isEnable) {
      await phyGroupApi.SetIsEnable(row.Id, isEnable)
      this.$message({
        type: 'success',
        message: '设置成功!'
      })
      // eslint-disable-next-line require-atomic-updates
      row.IsEnable = isEnable
    },
    drop(row) {
      const title = '确认删服务组 ' + row.GroupName + '?'
      const that = this
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.submitDrop(row.Id)
      })
    },
    reset() {
      this.queryParam.GroupName = null
      this.paging.Index = 1
      this.loadTable()
    },
    async submitDrop(id) {
      await phyGroupApi.Delete(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.loadTable()
    },
    async loadTable() {
      const res = await phyGroupApi.Query(this.queryParam, this.paging)
      if (res.List) {
        this.groups = res.List
      } else {
        this.groups = []
      }
      this.paging.Total = res.Count
    }
  }
}
</script>
